package com.steamedfish.auctionsecurityserveroauth2.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.steamedfish.auctionsecurityserveroauth2.entiry.UserEntiry;
import com.steamedfish.auctionsecurityserveroauth2.model.UserModel;
import com.steamedfish.auctionsecurityserveroauth2.service.SendSMS;
import com.steamedfish.auctionsecurityserveroauth2.service.UserService;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 高山福
 * @since 2021-03-22
 */
@RestController
@RequestMapping("/auctionsecurityserveroauth2/userEntiry")
public class UserController {

    @Autowired
    private SendSMS sendSMS;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private UserService userService;

    @DS("steamed_fish_auction_security")
    @RequestMapping(value = "/register")
    public ResponseEntity<?> register(@RequestBody() UserModel data,
                                      HttpServletRequest request,
                                      HttpServletResponse response) {

        //获取用户信息

        //获取验证码
        String codeInRedis = redisTemplate.opsForValue().get(data.getTelephone());
        Integer code = data.getCode();

        if (codeInRedis == null || codeInRedis.equals("")) {
            return ResponseEntity.ok("验证码已过期");
        }

        if (code == null || code.equals("")) {
            return ResponseEntity.ok("验证码错误");
        }

        //校验验证码
        if (Integer.parseInt(codeInRedis) != code) {
            return ResponseEntity.ok("验证码错误");
        }
        //校验用户信息
        if (data == null || (data.getMail() == null && data.getTelephone() == null)) {
            return ResponseEntity.ok("请输入手机号或者邮箱");
        }

        //封装用户信息
        UserEntiry userEntiry = new UserEntiry();
        if (!data.getTelephone().contains("@")) {
            BeanUtils.copyProperties(data, userEntiry);
        } else {
            BeanUtils.copyProperties(data, userEntiry);
            userEntiry.setMail(userEntiry.getTelephone());
            userEntiry.setTelephone("");
        }

        //注册户信息插入

        userService.register(userEntiry);

        //返回注册结果
        return ResponseEntity.ok("success");
    }


    @RequestMapping(value = "/getcode")
    public ResponseEntity<?> getCode(@RequestParam String telephone){

        //校验手机号是否合法
//        String coded = redisTemplate.opsForValue().get(telephone);

        //生成验证码
        String code = RandomStringUtils.randomNumeric(5);

        if (telephone.contains("@")) {

            sendSMS.sendByMail(telephone, code);
            return ResponseEntity.ok("success");
        }

        if (telephone.length() != 11) {
            throw new RuntimeException("手机号不存在");
        }

        //请求发送验证码
        sendSMS.send(telephone, code);

        return ResponseEntity.ok("success");
    }

    @RequestMapping(value = "/userinfo")
    @DS("steamed_fish_auction_security")
    public ResponseEntity<?> getUserInfo(){
        //获取用户id
        //获取用户id
        String json = SecurityContextHolder.getContext().getAuthentication().getName();
        UserEntiry userEntiry = JSON.parseObject(json, UserEntiry.class);

//        QueryWrapper queryWrapper = new QueryWrapper();
//        queryWrapper.eq("id", userEntiry.getId());
//
//        UserEntiry userEntiry1 = userService.getOne(queryWrapper);

        return ResponseEntity.ok(userEntiry);
    }
}
